﻿namespace ElisVK.Utils.HotKeys
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Windows.Input;
    using Application = System.Windows.Application;

    [Serializable]
    public class HotKeysCollection : IDisposable
    {
        public List<HotKey> HotKeys;

        public HotKeysCollection() {}

        public void InitializeByDefault()
        {
            HotKeys = new List<HotKey>(new HotKey[]
                {
                    new HotKey("Play/Pause", ModifierKeys.Control | ModifierKeys.Shift, Keys.Home),
                    new HotKey("Play/Pause", ModifierKeys.None, Keys.MediaPlayPause),
                    new HotKey("Previous", ModifierKeys.Control | ModifierKeys.Shift, Keys.PageUp),
                    new HotKey("Previous", ModifierKeys.None, Keys.MediaPreviousTrack),
                    new HotKey("Next", ModifierKeys.Control | ModifierKeys.Shift, Keys.PageDown),
                    new HotKey("Next", ModifierKeys.None, Keys.MediaNextTrack),
                    new HotKey("Volume Up", ModifierKeys.Control | ModifierKeys.Shift, Keys.Up),
                    new HotKey("Volume Down", ModifierKeys.Control | ModifierKeys.Shift, Keys.Down),
                    new HotKey("Add", ModifierKeys.Control | ModifierKeys.Shift, Keys.Insert),
                    new HotKey("Remove", ModifierKeys.Control | ModifierKeys.Shift, Keys.Delete)
                });
        }

        public void RegAllHotKeys(Action<HotKey> hotKeyPressed)
        {
            foreach (var hotKey in HotKeys)
            {
                try
                {
                    hotKey.RegisterHotKey(Application.Current.MainWindow);
                    hotKey.HotKeyPressed += hotKeyPressed;
                }
                catch (Exception)
                {
                }
                
            }
        }

        public void UnRegAllHotKeys()
        {
            foreach (var hotKey in HotKeys)
            {
                hotKey.UnregisterHotKey();
            }
        }

        //public bool ChangeHotKey(HotKey hotKey, KeyModifiers modifiers, Keys key)
        //{
        //    for (int i = 0; i < HotKeys.Length; i++)
        //    {
        //        if (HotKeys[i].Name == hotKey.Name)
        //        {
        //            HotKeys[i] = new HotKey(hotKey.Name, modifiers, key);

        //            try
        //            {
        //                UnRegHotKey(hotKey);
        //                RegHotKey(HotKeys[i]);
        //            }
        //            catch (Exception e)
        //            {
        //                MessageBox.Show(e.Message);
        //                return false;
        //            }

        //            break;
        //        }
        //    }
        //    return true;
        //}

        public void Dispose()
        {
            foreach (var hotKey in HotKeys)
            {
                hotKey.Dispose();
            }
        }
    }
}
